package de.hawhamburg.shc.controller;




import de.hawhamburg.smarthomecontroller.R;
import de.hawhamburg.shc.livingplace.JSONHelper;
import de.hawhamburg.shc.livingplace.SendMessageToProxy;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class RoomDetailActivity extends Activity {
	

    static String ROOMNAME_LOUNGE = "lounge";
    static String ROOMNAME_KITCHEN = "kitchen";
    static String ROOMNAME_DINING = "dining";
    static String ROOMNAME_SLEEPING = "sleeping";
	static String ROOMNAME_BATH = "bathroom";
    static String ROOMNAME_CORRIDOR = "corridor";
    
	static String ROOM_BATH_WIN = "winBathroom";
	
	static int MAX_POSITION_FOR_WINDOWS = 20;
	int layout;
	String layoutName;
	String room;
	String germanRoomName;
	String roomLight1;
	String roomLight2;
	String roomCurtain1;
	String roomCurtain2;
	
	TextView roomText;
	Switch light1;
	Switch light2;
	Switch heating;
	Switch curtain1;
	Switch curtain2;
	Switch blinds;
	SeekBar windows;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		if (extras != null){
			layout = extras.getInt("layout");
			room = extras.getString("roomName");
			germanRoomName = extras.getString("german");
			
			switch(layout){
			case 0:
				setContentView(R.layout.room_fragment_lounge);
				//Text
				roomText = (TextView) findViewById(R.id.text1);
		        roomText.setText(germanRoomName);
				//Lights
		        roomLight1 = room;
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);
		        //Heating
		        heating = (Switch) findViewById(R.id.heating);
		        heating.setOnCheckedChangeListener(heatingListener);
		        //Windows
		        windows = (SeekBar) findViewById(R.id.windows);
		        windows.setOnSeekBarChangeListener(windowsListener);
		        //Curtains
		        roomCurtain1 = room;
		        curtain1 = (Switch) findViewById(R.id.curtain1);
		        curtain1.setOnCheckedChangeListener(curtain1Listener);
		        //Blinds
		        blinds = (Switch) findViewById(R.id.blind1);
		        blinds.setOnCheckedChangeListener(blindsListener);
		        break;

			case 1:
				setContentView(R.layout.room_fragment_kitchen);
				
				//Text
				roomText = (TextView) findViewById(R.id.text1);
		        roomText.setText(germanRoomName);
		        //Main lights
		        roomLight1 = room + extras.getString("kitchenMain");
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);
		        //Cooking lights
		        roomLight2 = room + extras.getString("kitchenCooking");
		        light2 = (Switch) findViewById(R.id.light2);
		        light2.setOnCheckedChangeListener(light2Listener);
		        //Heating
		        heating = (Switch) findViewById(R.id.heating);
		        heating.setOnCheckedChangeListener(heatingListener);
		        //Windows
		        windows = (SeekBar) findViewById(R.id.windows);
		        windows.setOnSeekBarChangeListener(windowsListener);
		        //Blinds
		        blinds = (Switch) findViewById(R.id.blind1);
		        blinds.setOnCheckedChangeListener(blindsListener);
				break;			
			case 2:
				setContentView(R.layout.room_fragment_dining);
				//Text
				roomText = (TextView) findViewById(R.id.text1);
		        roomText.setText(germanRoomName);
				//Lights
		        roomLight1 = room;
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);
		        //Heating
		        heating = (Switch) findViewById(R.id.heating);
		        heating.setOnCheckedChangeListener(heatingListener);
		        //Windows
		        windows = (SeekBar) findViewById(R.id.windows);
		        windows.setOnSeekBarChangeListener(windowsListener);
		        //Blinds
		        blinds = (Switch) findViewById(R.id.blind1);
		        blinds.setOnCheckedChangeListener(blindsListener);
		        break;
			case 3:
				setContentView(R.layout.room_fragment_sleeping);
				//Text
				roomText = (TextView) findViewById(R.id.text1);
		        roomText.setText(germanRoomName);
		        //Lights
		        roomLight1 = room;
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);
		        //Heating
		        heating = (Switch) findViewById(R.id.heating);
		        heating.setOnCheckedChangeListener(heatingListener);
		        //Curtains
		        roomCurtain1 = room+"_window";
		        roomCurtain2 = room+"_hall";
		        curtain1 = (Switch) findViewById(R.id.curtain1);
		        curtain1.setOnCheckedChangeListener(curtain1Listener);
		        curtain2 = (Switch) findViewById(R.id.curtain2);
		        curtain2.setOnCheckedChangeListener(curtain2Listener);
		        //Blinds
		        blinds = (Switch) findViewById(R.id.blind1);
		        blinds.setOnCheckedChangeListener(blindsListener);
				break;	
			case 4:
				setContentView(R.layout.room_fragment_bathroom);
				//Text
				roomText = (TextView) findViewById(R.id.text1);
		        roomText.setText(germanRoomName);
				//Lights
		        roomLight1 = room;
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);
		        //Heating
		        heating = (Switch) findViewById(R.id.heating);
		        heating.setOnCheckedChangeListener(heatingListener);
		        //Windows
		        windows = (SeekBar) findViewById(R.id.windows);
		        windows.setOnSeekBarChangeListener(windowsListener);
		        break;
			case 5:
				setContentView(R.layout.room_fragment_corridor);
				//Text
				roomText = (TextView) findViewById(R.id.text1);	
		        roomText.setText(germanRoomName);
				//Lights
		        roomLight1 = room;
				light1 = (Switch) findViewById(R.id.light1);
		        light1.setOnCheckedChangeListener(light1Listener);			
				break;
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.room_detail, menu);
		return true;
	}
	
	public OnCheckedChangeListener light1Listener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {	      
			 new SendMessageToProxy().execute(JSONHelper.createLightMessage(roomLight1, isChecked)); 
		}
	};
	
	public OnCheckedChangeListener light2Listener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {		          
		    new SendMessageToProxy().execute(JSONHelper.createLightMessage(roomLight2, isChecked));    
		}
	};
	public OnCheckedChangeListener heatingListener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked == true){
					System.out.println("Heating an");
					//new SendMessageToProxy().execute(JSONHelper.createHeatingMessage("HEATING.CONTROL", "module", "hexValue"));
				}else{
					System.out.println("Heating aus");
					//new SendMessageToProxy().execute(JSONHelper.createHeatingMessage("HEATING.CONTROL", "module", "hexValue"));
		        }	
		}
	};
	
	
	public OnSeekBarChangeListener windowsListener = new OnSeekBarChangeListener(){
		double currentValue;
		double targetPosition;
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
			//calculate the centimeter value to open or close the windows 
			currentValue = seekBar.getProgress();
			targetPosition = getTargetPositionFromCurrentValue(currentValue);			
			int value = new Double(targetPosition).intValue();

			if(room.equals(ROOMNAME_BATH)){
				
				new SendMessageToProxy().execute(JSONHelper.createWindowMessageString(ROOM_BATH_WIN,value));
			}else{
				
			}
			new SendMessageToProxy().execute(JSONHelper.createWindowMessageString(room.toUpperCase(),value));        
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {}
	};
	
	/*
	 * this method calculates the centimeter value to open or close the windows 
	 */
	private double getTargetPositionFromCurrentValue(double currentValue){
		double result;
		int maxPosition = MAX_POSITION_FOR_WINDOWS;
		result = maxPosition * (currentValue/100);
		double finalValue = Math.round( result * 100.0 ) / 100.0;
		return finalValue;
	}
	
	public OnCheckedChangeListener curtain1Listener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {		        
			 new SendMessageToProxy().execute(JSONHelper.createCurtainMessage(roomCurtain1, isChecked));
	       
		}	
	 };	

	public OnCheckedChangeListener curtain2Listener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {			
			 new SendMessageToProxy().execute(JSONHelper.createCurtainMessage(roomCurtain2, isChecked));
	        
		}	
	 };
	 
	 OnCheckedChangeListener blindsListener = new OnCheckedChangeListener() {
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(room.contains(ROOMNAME_DINING) || room.contains(ROOMNAME_KITCHEN)) room = ROOMNAME_DINING+"_"+ROOMNAME_KITCHEN;
	        new SendMessageToProxy().execute(JSONHelper.createBlindMessage(room, isChecked)); 
		}	
	 };	
	 
	 @Override
		protected void onPause() {
			super.onPause();
			SharedPreferences buttonStates =	getSharedPreferences(room, 0);
		      SharedPreferences.Editor editor = buttonStates.edit();
		      if(light1 != null) editor.putBoolean("light1", light1.isChecked());
		     if(light2 != null) editor.putBoolean("light2", light2.isChecked());
		     if(heating != null)  editor.putBoolean("heating", heating.isChecked());
		     if(curtain1 != null) editor.putBoolean("curtain1", curtain1.isChecked());
		     if(curtain2 != null) editor.putBoolean("curtain2", curtain2.isChecked());
		     if(blinds != null) editor.putBoolean("blinds", blinds.isChecked());
		     if(windows != null) editor.putInt("windows", windows.getProgress());
		      editor.commit();
		
		}
		
	 
	 

	    @Override
	    protected void onResume(){
	       super.onResume();
	     
	       // Restore preferences
	       SharedPreferences buttonStates = getSharedPreferences(room, 0);
	       if(light1 != null)  light1.setChecked(buttonStates.getBoolean("light1", false));
	       if(light2 != null)  light2.setChecked(buttonStates.getBoolean("light2", false));
	       if(heating != null) heating.setChecked(buttonStates.getBoolean("heating", false));
	       if(curtain1 != null) curtain1.setChecked(buttonStates.getBoolean("curtain1", false));
	       if(curtain2 != null)curtain2.setChecked(buttonStates.getBoolean("curtain2", false));
	       if(blinds != null) blinds.setChecked(buttonStates.getBoolean("blinds", false));
	       if(windows != null) windows.setProgress(buttonStates.getInt("windows", 0));      
	       
	    }
}
